package com.zyf.retrofitdemo.proxy;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * AOP 动态代理
 *
 * @author zyf
 * @date 2018/2/28
 * @time 10:06
 */

public class JDKDynamicProxy implements InvocationHandler {
    private Object realObject;

    public JDKDynamicProxy(Object realObject) {
        this.realObject = realObject;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("-------------------------------------------------");
        System.out.println("JDKDynamicProxy Method " + method.toString());

        parseMethodAnnotations(method.getDeclaredAnnotations());
        parseParamAnnotations(method.getParameterAnnotations());


        if (args != null) {
            System.out.println("JDKDynamicProxy Args " + args.toString());
        }
        return method.invoke(realObject, args);
    }


    private void parseMethodAnnotations(Annotation[] annotations) {
        for (Annotation a : annotations) {
            if (a instanceof ZMethod) {
                System.out.println("ZMethod  " + ((ZMethod) a).value());
            }
        }
    }

    private void parseParamAnnotations(Annotation[][] annotations) {
        for (Annotation[] a : annotations) {
            for (Annotation a1 : a) {
                if (a1 instanceof ZParam) {
                    System.out.println("ZParam  " + ((ZParam) a1).name() + "  " + ((ZParam) a1).value());
                }
            }
        }
    }
}
